package com.bainarm.testteabarmachine;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.zxing.BarcodeFormat;

import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;


public class PersonnelInformationFillingActivity extends AppCompatActivity {

    private static final String TAG = "pifa";
    private Button generateQRCodeButton;
    private TextView returnMain;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personnel_information_filling);
        generateQRCodeButton = findViewById(R.id.generateQRCodeButton);
        generateQRCodeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 生成二维码
                String name = "John Doe";
                String phone = "1234567890";
                String qrText = "Name: " + name + "\nPhone: " + phone;

                // 显示二维码弹窗
                try {
                    Bitmap qrCode = generateQRCode(qrText);
                    showQRCodeDialog(qrCode);
                } catch (Exception e) {
                    Log.e(TAG, "Error generating QR code: " + e.getMessage());
                    e.printStackTrace();
                    // 这里可以添加更多异常处理的逻辑
                }
            }
        });

        // 返回首页
        returnMain = findViewById(R.id.returnMain);
        returnMain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navigateToMainActivity();
            }
        });
    }

    // 生成二维码
    private Bitmap generateQRCode(String qrText) {
        Bitmap bitmap = null;
        try {
            QRCodeWriter writer = new QRCodeWriter();
            BitMatrix bitMatrix = writer.encode(qrText, BarcodeFormat.QR_CODE, 512, 512); // 调整大小为需要的尺寸

            int width = bitMatrix.getWidth();
            int height = bitMatrix.getHeight();
            int[] pixels = new int[width * height];

            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * width + x] = 0xFF000000; // 黑色
                    } else {
                        pixels[y * width + x] = 0xFFFFFFFF; // 白色
                    }
                }
            }

            bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        } catch (WriterException e) {
            // 错误处理
            e.printStackTrace();
        }
        return bitmap;
    }


    // 显示二维码弹窗
    private void showQRCodeDialog(Bitmap qrCode) {
        Log.e(TAG, "showQRCodeDialog");
        // 创建弹窗
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.dialog_qr_code, null);
        ImageView qrCodeImageView = dialogView.findViewById(R.id.qrCodeImageView);
        qrCodeImageView.setImageBitmap(qrCode);
        builder.setView(dialogView);

        // 显示弹窗
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    private void navigateToMainActivity() {
        Intent intent = new Intent(PersonnelInformationFillingActivity.this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        // TODO 清理登录记录
        finish();  // 结束当前活动
    }
}